$.extend({

    tabs : null,

    check_tree : function(){
        $("li.ui-state-default.none").removeClass("none");
        /** remove buttons "up" and "down" */
        $("div.pom").each(function(){
            $(this).children("div.pom_parent.main").first().children("div.panel").children("ul").children("li").children("span.ui-icon-arrowthick-1-n").parent().addClass("none");
            $(this).children("div.pom_parent.main").last().children("div.panel").children("ul").children("li").children("span.ui-icon-arrowthick-1-s").parent().addClass("none");
        });
    },

    /** setting tabs */
    init_tabs : function(){
        var active = $.cookie("tab");
        if (typeof active == "undefined") active = 0;
        $.tabs = $("#tabs").tabs({
            heightStyle : "content",
            active      : active
        });

        // close icon: removing the tab on click
        $.tabs.delegate( "span.ui-icon-close", "click", function() {
            var panelId = $( this ).closest( "li" ).remove().attr( "aria-controls" );
            $( "#" + panelId ).remove();
            $.tabs.tabs( "refresh" );
        });
    },

    addTab : function(id_content, text){
        var label = "Редаг. конт. [id = "+id_content+"]",
            count = $("#tabs > div").length,
            id = "tabs-" + (count+1),
            li = '<li><a href="#'+id+'" title="'+text+'">'+label+'</a> <span class="ui-icon ui-icon-close" role="presentation">Remove Tab</span></li>';

        $.ajax({
            url : $.ajax_path+'content/get_content.php',
            dataType : "json",
            data : {id:id_content},
            success: function(d){
                var c = $("<div/>", {
                    id: id,
                    html: "<div class='content_editor'>"+d.html+"</div>"
                });
                $.tabs.children( ".ui-tabs-nav" ).append( li );
                c.appendTo($.tabs);
                $.tabs.tabs( "refresh" );
                $.tabs.tabs( "option", "active", count );

                $.init_js(d.type, c);
//                $(do)
            },
            error: function(data){
                console.log(data);
            }
        });
    },

    init_js : function(type, content){
        if (type == 0){ // blog content
            $("#save_html")
                .button()
                .click(function(){
                    var id = parseInt( $("#save_html").attr("data-id") );
                    $(this).parent("form").ajaxForm({
                        url: $.ajax_path+"content/set_content.php",
                        data: {
                            id : id,
                            c : tinyMCE.activeEditor.getContent()
                        },
                        dataType: "json",
                        success: function(data){

                        },
                        error: function(data){
                            console.log(data);
                        }


                    }).submit();
                });
            tinyMCE.remove();
            tinyMCE.init({
                // General options
                selector: "textarea.html_content",
                mode : "textareas",
                theme : "modern",
                language : "uk_UA",
                image_advtab: true,
                image_class_list: [
                    {title: 'Float left',   value: 'left'},
                    {title: 'Float right1', value: 'right'}
                ],
                height : 400,
                plugins : [
                    "advlist autolink lists link image charmap print preview hr anchor pagebreak",
                    "searchreplace wordcount visualblocks visualchars code fullscreen",
                    "insertdatetime jbimages nonbreaking save table contextmenu directionality",
                    "emoticons template paste textcolor colorpicker textpattern"
                ],

                toolbar1: "save | insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image jbimages",
                toolbar2: "forecolor backcolor emoticons",
                image_advtab: true
            });


        }
    },

    // new window
    add_new_window : function(){
        var count = $("div.window"),
            e = $("<div/>", {
                "class" : "window",
                id : "window_"+count
            }).appendTo($("body"));
        return e;
    },

    errors : [],
    hide_error : function(e, time){
        if (e.hasClass("hover")){
            $.hide_error(e, time);
        }else{
            e.fadeOut(300);
            setTimeout(function(){
                e.remove();
            },350);
        }
    },
    error_window : function(options){
        var w = $('<div/>',{
                'class' : 'ui-state-error ui-corner-all',
                'padding' : '0 .7em;'
            }).appendTo($("body")),
            p = $('<p/>').appendTo(w),
            icon = $("<span/>",{
                'class' : 'ui-icon ui-icon-alert',
                'style' : 'float: left; margin: 0 .3em;'
            }).appendTo(p);

        $('<strong/>',{html:'CMS - error: '}).appendTo(p);
        p.append(options);
        w.css({
                position : 'fixed',
                bottom : "1%",
                width : "98%",
                marginLeft : "1%",
                display: 'none'
            })
            .delay(200).show("blind", {direction:"vertical"}, 1000)
            .hover(
                function(){
                    $(this).addClass('hover');
                },
                function(){
                    $(this).removeClass('hover');
                }
            );
        setTimeout(function(){
            $.hide_error(w, 5000);
        },5000);
        $.errors.push({type: 0, value: options});
        $("<li/>",{
            'class' : 'error',
            'html'  : options
        }).appendTo( $("#log_history > ol") );
    },

    ajax_path : "/rapatij-cms/vikno.com.cv.ua/admin/ajax/"

});